extends Node2D

@onready var gun: Sprite2D = $Gun
var enemies = []
var bullet = preload("res://Bullet.tscn")
var current_enemies
var building = true

func _on_sight_area_entered(area: Area2D) -> void:
	if area.is_in_group("Enemy"):
		enemies.append(area)




func _on_sight_area_exited(area: Area2D) -> void:
	if area.is_in_group("Enemy"):
		enemies.erase(area)

func _physics_process(delta: float) -> void:
	#当建造行为标志为false时才开始工作
	if building == false:
		if enemies != []:
			current_enemies = enemies[0]
			gun.look_at(current_enemies.global_position)
	else :
		global_position = get_global_mouse_position()
		if Input.is_action_just_pressed("click"):
			building = false
			get_parent().tower_built()

func _on_shoot_timer_timeout() -> void:
	if current_enemies:
		if enemies:
			if current_enemies == enemies[0]:
				var b = bullet.instantiate()
				b.global_position = global_position
				b.target = current_enemies
				get_parent().add_child(b)
